package com.qys.training.base.util;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

public class BaseUtils {

    public static String stringToMD5(String context) {
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            byte[] result = md.digest(context.getBytes());
            return Base64.encode(result);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String fileToMD5(File file) {
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            byte[] result = md.digest(getByte(file));
            return Base64.encode(result);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }
  
    public static String getUuid() {
        String uuid = UUID.randomUUID().toString().replace("-","");
        return uuid;
    }

    private static byte[] getByte(File file) {
        // 得到文件长度
        byte[] b = new byte[(int) file.length()];
        return b;
    }

}
